package ge.telasi.tasks.ui.comp;

/**
 * @author dimitri
 */
public abstract class PageableTable<T> extends SimpleTable<T> {
    private int page;
    private int perPage = 50;
    private int totalCount;

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getPerPage() {
        return perPage;
    }

    public void setPerPage(int perPage) {
        this.perPage = perPage;
    }

    public int getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }

    @Override
    public void clearData() {
        super.clearData();
        totalCount = 0;
    }
    
    /**
     * calculate maximum page.
     */
    public int getMaxPage() {
        int maxPage = 0;
        if (perPage > 0) {
            maxPage = totalCount / perPage;
        }
        if (totalCount > (maxPage + 1) * perPage) {
            maxPage++;
        } else if (totalCount == maxPage * perPage) {
            maxPage--;
        }
        return maxPage;
    }

    public boolean isFirstPage() {
        return page == 0;
    }

    public boolean isLastPage() {
        return page == getMaxPage();
    }

    public void next() {
        if (!isLastPage()) {
            page++;
            refresh();
        }
    }

    public void previous() {
        if (!isFirstPage()) {
            page--;
            refresh();
        }
    }

}
